<?php

namespace app\base\controller;

use app\common\controller\Base;
use think\Request;

class Department extends Base {
  protected $model;

  public function __construct(Request $request = NULL) {
    parent::__construct($request);
    $this->model = new \app\base\model\Department();
  }

  public function _initialize() {
    parent::_initialize();
  }

  public function index() {
    return view();
  }

  public function manager() {
    return view();
  }

  public function treeDepartment($manager = NULL) {
    $depts = $this->model->order('sort DESC,id ASC')
      ->column('parent_id,title,manager_uid,is_hide,is_branch,short,sort', 'id');

    return json(self::jsTree($depts, TRUE));
  }

  private function jsTree($tree) {
    $arr = [];
    foreach ($tree as $k => $v) {
      $text = "{$v['title']}";
      $text .= (!empty($v['sort'])) ? '<span class="text-warning">【排序：' . $v['sort'] . '】</span>' : '';
      $arr[] = ['id'     => $v['id'],
                'parent' => $v['parent_id'] == 0 ? "#" : $v['parent_id'],
                'text'   => $text,
                //'is_hide'   => $v['is_hide'],
                'icon'   => ($v['is_hide'] == 1) ? "fa fa-window-close" : ($v['icon'] ? 'fa ' . $v['icon'] : ''),
      ];
    }

    return $arr;
  }

  public function rename() {
    $post = input('post.');
    $data = ['parent_id' => $post['parent'],
             'title'     => $post['text']];
    $logid = 0;
    if (is_numeric($post['id'])) {
      $logid = $data['id'] = $post['id'];
      $this->model->isUpdate(TRUE);
    }
    $r = $this->model->save($data);
    if ($r === FALSE) {
      $this->error(lang('error'));
    }


    $this->success(lang('success'));
  }

  public function edit() {
    $id = input('id/d', 0, 'abs');
    $row = $this->model->find($id);
    if (!request()->isPost()) {
      $this->assign('departments', $this->model->select());
      $this->assign('row', $row);
      $this->assign('id', $id);

      return view('edit');
    } else {
      $form = input('form/a');
      $r = $this->model->edit($form, $id);
      if ($r === TRUE) {
        $this->success(lang('success'), url('index'));
      } else {
        $this->error($r);
      }
    }
  }

  public function delete() {
    $id = input('id/d', 0);
    if ($id == 0) $this->error('ID错误');
    $subs = $this->model->where('parent_id', $id)->count();
    if ($subs === FALSE) $this->error('查找部门失败');
    if ($subs > 0) $this->error('该部门含有子部门，请先删除');
    $r = $this->model->where('id', $id)->delete();
    if ($r !== FALSE) {

      $this->success(lang('success'));
    } else {
      $this->error($this->model->getError());
    }
  }

  public  function dept_short() {
    $parent_id = input('id/d', 1, 'abs');
    $branch = input('branch/d', 1);
    if ($parent_id == 1) {
      $this->result($this->model->dept_short($parent_id, 0));
    } else {
      $this->result($this->model->dept_short($parent_id));
    }
  }

  public  function lists() {
    if (request()->isAjax() == TRUE) {
      $action = request()->action() . '_ajax';
      $this->result($this->$action());
    }

    return view();
  }

  private  function lists_ajax() {
    $rows = $this->model->column('title,parent_id,is_branch,desc', 'id');
    $data = [];
    foreach ($rows as $id => $row) {
      $data[] = [
        'id'          => $id,
        'parent_name' => $rows[ $row['parent_id'] ]['title'],
        'name'        => $row['title'],
        'desc'        => $row['desc'],
        'desc'        => $row['desc'],
      ];
    }

    return $data;
  }

}
